package tcpserver.handler.in;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import tcpserver.RFIDSession;
import tcpserver.RFIDSessionManager;

public class SessionHandler extends ChannelInboundHandlerAdapter {

    private final static RFIDSessionManager manager = RFIDSessionManager.instance;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        manager.computeIfAbsent(ctx.channel(), (__) -> new RFIDSession(ctx.channel()));

        ctx.fireChannelActive();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        ctx.channel().close(ctx.newPromise().addListener((__) -> manager.remove(ctx.channel())));
    }
}
